{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "# 5.2 观察迭代中的振荡"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "假设一元凸函数的方程为$f(x)=x^2+2$，在利用迭代法求方程极小值的过程中，完成以下任务：\n",
    "\n",
    "- 取x的初始值为3，步长为0.5，迭代次数为8，观察计算过程。\n",
    "- 取x的初始值为3，步长为0.7，迭代次数为8，观察计算过程。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8d7baa9c-93a2-42f3-a3c1-231cdb587f2d",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b77a80eb-a596-4410-8b1f-5322e1d141a3",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "de0514a5-6568-421e-afe4-f38d5dc6f93f",
   "metadata": {},
   "source": [
    "设定一个相对合理的迭代步长，可以使程序通过迭代找到函数对应的极值点。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fun_iter(step=0.5):\n",
    "    # x的初始值\n",
    "    x=3    \n",
    "    iter=8    \n",
    "    for i in range(0,iter):\n",
    "    #     y的当前值\n",
    "        y=x**2+2\n",
    "    #     向左调整\n",
    "        x1=x-step\n",
    "        y1=x1**2+2\n",
    "    #     向右调整\n",
    "        x2=x+step    \n",
    "        y2=x2**2+2\n",
    "        print('第',i,'轮:','x:','%.2f'%x,',y:','%.2f'%y,',x1:','%.2f'%x1,',y1:','%.2f'%y1,',x2:','%.2f'%x2,',y2:','%.2f'%y2)\n",
    "    #     更新x0\n",
    "        if y1 < y:\n",
    "            x = x1\n",
    "        if y2 < y:\n",
    "            x = x2\n",
    "        y=x**2 + 2    \n",
    "        print('本轮调整后的x:','%.2f'%x,'函数值y:','%.2f'%y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "29ed7479-b39f-4304-b5c9-34b705e9da13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 0 轮: x: 3.00 ,y: 11.00 ,x1: 2.50 ,y1: 8.25 ,x2: 3.50 ,y2: 14.25\n",
      "本轮调整后的x: 2.50 函数值y: 8.25\n",
      "第 1 轮: x: 2.50 ,y: 8.25 ,x1: 2.00 ,y1: 6.00 ,x2: 3.00 ,y2: 11.00\n",
      "本轮调整后的x: 2.00 函数值y: 6.00\n",
      "第 2 轮: x: 2.00 ,y: 6.00 ,x1: 1.50 ,y1: 4.25 ,x2: 2.50 ,y2: 8.25\n",
      "本轮调整后的x: 1.50 函数值y: 4.25\n",
      "第 3 轮: x: 1.50 ,y: 4.25 ,x1: 1.00 ,y1: 3.00 ,x2: 2.00 ,y2: 6.00\n",
      "本轮调整后的x: 1.00 函数值y: 3.00\n",
      "第 4 轮: x: 1.00 ,y: 3.00 ,x1: 0.50 ,y1: 2.25 ,x2: 1.50 ,y2: 4.25\n",
      "本轮调整后的x: 0.50 函数值y: 2.25\n",
      "第 5 轮: x: 0.50 ,y: 2.25 ,x1: 0.00 ,y1: 2.00 ,x2: 1.00 ,y2: 3.00\n",
      "本轮调整后的x: 0.00 函数值y: 2.00\n",
      "第 6 轮: x: 0.00 ,y: 2.00 ,x1: -0.50 ,y1: 2.25 ,x2: 0.50 ,y2: 2.25\n",
      "本轮调整后的x: 0.00 函数值y: 2.00\n",
      "第 7 轮: x: 0.00 ,y: 2.00 ,x1: -0.50 ,y1: 2.25 ,x2: 0.50 ,y2: 2.25\n",
      "本轮调整后的x: 0.00 函数值y: 2.00\n"
     ]
    }
   ],
   "source": [
    "fun_iter(step=0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5c56c86-78a3-4825-b829-8e1b4453d004",
   "metadata": {},
   "source": [
    "可见，步长为0.5时，第5轮达到极值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "daaabee1-06a8-4c29-b90d-4eb4ca3ae1b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 0 轮: x: 3.00 ,y: 11.00 ,x1: 2.30 ,y1: 7.29 ,x2: 3.70 ,y2: 15.69\n",
      "本轮调整后的x: 2.30 函数值y: 7.29\n",
      "第 1 轮: x: 2.30 ,y: 7.29 ,x1: 1.60 ,y1: 4.56 ,x2: 3.00 ,y2: 11.00\n",
      "本轮调整后的x: 1.60 函数值y: 4.56\n",
      "第 2 轮: x: 1.60 ,y: 4.56 ,x1: 0.90 ,y1: 2.81 ,x2: 2.30 ,y2: 7.29\n",
      "本轮调整后的x: 0.90 函数值y: 2.81\n",
      "第 3 轮: x: 0.90 ,y: 2.81 ,x1: 0.20 ,y1: 2.04 ,x2: 1.60 ,y2: 4.56\n",
      "本轮调整后的x: 0.20 函数值y: 2.04\n",
      "第 4 轮: x: 0.20 ,y: 2.04 ,x1: -0.50 ,y1: 2.25 ,x2: 0.90 ,y2: 2.81\n",
      "本轮调整后的x: 0.20 函数值y: 2.04\n",
      "第 5 轮: x: 0.20 ,y: 2.04 ,x1: -0.50 ,y1: 2.25 ,x2: 0.90 ,y2: 2.81\n",
      "本轮调整后的x: 0.20 函数值y: 2.04\n",
      "第 6 轮: x: 0.20 ,y: 2.04 ,x1: -0.50 ,y1: 2.25 ,x2: 0.90 ,y2: 2.81\n",
      "本轮调整后的x: 0.20 函数值y: 2.04\n",
      "第 7 轮: x: 0.20 ,y: 2.04 ,x1: -0.50 ,y1: 2.25 ,x2: 0.90 ,y2: 2.81\n",
      "本轮调整后的x: 0.20 函数值y: 2.04\n"
     ]
    }
   ],
   "source": [
    "fun_iter(step=0.7)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f215b184-b19b-499d-ae5e-e46add441a98",
   "metadata": {},
   "source": [
    "可见，步长为0.7时，从第4轮之后，函数值保持在2.04，没能收敛到极值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "947bd149-004c-4238-863f-ba125a57b79b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
